#!/usr/bin/env python3
# minitest -m indexcase -c config.json -g
import minium, threading, time
import random
class IndexTest(minium.MiniTest):
    def test_index(self):
        def test_map_margin():
            emap = self.page.get_element(selector="//*[@id=\"map\"]")
            for i in range(4):
                rect = emap.rect
                print(rect)
                x_offset = random.randint(100, 400)
                y_offset = random.randint(100, 400)
                offset = emap.offset
                size = emap.size
                page_offset = emap.page_offset
                ori_changed_touch = ori_touch = {
                    "identifier": 0,
                    "pageX": offset["left"] + size["width"] // 2,
                    "pageY": offset["top"] + size["height"] // 2,
                    "clientX": offset["left"] + size["width"] // 2 - page_offset.x,
                    "clientY": offset["top"] + size["height"] // 2 - page_offset.y,
                }
                emap.touch_start(touches=[ori_touch], changed_touches=[ori_changed_touch])
                time.sleep(2)
                changed_touch = touch = {
                    "identifier": 0,
                    "pageX": offset["left"] + size["width"] // 2 + x_offset,
                    "pageY": offset["top"] + size["height"] // 2 + y_offset,
                    "clientX": offset["left"] + size["width"] // 2 - page_offset.x + x_offset,
                    "clientY": offset["top"] + size["height"] // 2 - page_offset.y + y_offset,
                }
                emap.touch_move(touches=[touch], changed_touches=[changed_touch])
                time.sleep(2)
                emap.touch_end(changed_touches=[changed_touch])
                time.sleep(2)
                self.assertDictEqual(
                    {
                        "left": rect["left"] + x_offset,
                        "top": rect["top"] + y_offset,
                        "width": rect["width"],
                        "height": rect["height"],
                    },
                    emap.rect,
                )
        
        def test_route():
            def move_map(emap):
                rect = emap.rect
                print(rect)
                x_offset = random.randint(100, 400)
                y_offset = random.randint(100, 400)
                offset = emap.offset
                size = emap.size
                page_offset = emap.page_offset
                ori_changed_touch = ori_touch = {
                    "identifier": 0,
                    "pageX": offset["left"] + size["width"] // 2,
                    "pageY": offset["top"] + size["height"] // 2,
                    "clientX": offset["left"] + size["width"] // 2 - page_offset.x,
                    "clientY": offset["top"] + size["height"] // 2 - page_offset.y,
                }
                emap.touch_start(touches=[ori_touch], changed_touches=[ori_changed_touch])
                time.sleep(2)
                changed_touch = touch = {
                    "identifier": 0,
                    "pageX": offset["left"] + size["width"] // 2 + x_offset,
                    "pageY": offset["top"] + size["height"] // 2 + y_offset,
                    "clientX": offset["left"] + size["width"] // 2 - page_offset.x + x_offset,
                    "clientY": offset["top"] + size["height"] // 2 - page_offset.y + y_offset,
                }
                emap.touch_move(touches=[touch], changed_touches=[changed_touch])
                time.sleep(2)
                emap.touch_end(changed_touches=[changed_touch])
                time.sleep(2)
                self.assertDictEqual(
                    {
                        "left": rect["left"] + x_offset,
                        "top": rect["top"] + y_offset,
                        "width": rect["width"],
                        "height": rect["height"],
                    },
                    emap.rect,
                )
            
            esrc = self.page.get_element(selector="input1")
            edst = self.page.get_element(selector="input2")
            emylocation = self.page.get_element(selector="/page/view/view[1]/view[1]/view[1]/image")
            esearch = self.page.get_element(selector="search-button")

            for _ in range(20):
                esrc.tap()
                time.sleep(1)
                move_map(esrc)
                self.app.navigate_back()
    
                edst.tap()
                time.sleep(1)
                move_map(edst)
                self.app.navigate_back()
    
                esearch.tap()
                time.sleep(1)
                self.app.navigate_back()

        # *********************
        indexpage = self.app.switch_tab("/pages/index/index")
        time.sleep(1)
        self.assertEqual(self.app.current_page.path, "/pages/index/index", "index path here")
    
        #test map margin
        test_map_margin()

        #test setting location
        test_route()
